package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Recommend;
import com.ruoyi.system.service.IRecommendService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 推荐帖审核Controller
 * 
 * @author ruoyi
 * @date 2025-06-05
 */
@RestController
@RequestMapping("/system/recommend")
public class RecommendController extends BaseController
{
    @Autowired
    private IRecommendService recommendService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询推荐帖审核列表
     */
    @PreAuthorize("@ss.hasPermi('system:recommend:list')")
    @GetMapping("/list")
    public TableDataInfo list(Recommend recommend)
    {
        System.out.println(recommend);
        if (recommend.getNickName()!=null){
            SysUser sysUser = sysUserService.selectUserByNickName(recommend.getNickName());
            System.out.println(sysUser);
            if (sysUser.getUserId()==null){
                return null;
            }
            recommend.setUserId(sysUser.getUserId());
        }
        startPage();
        List<Recommend> list = recommendService.selectRecommendList(recommend);
        return getDataTable(list);
    }

    /**
     * 导出推荐帖审核列表
     */
    @PreAuthorize("@ss.hasPermi('system:recommend:export')")
    @Log(title = "推荐帖审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Recommend recommend)
    {
        List<Recommend> list = recommendService.selectRecommendList(recommend);
        ExcelUtil<Recommend> util = new ExcelUtil<Recommend>(Recommend.class);
        util.exportExcel(response, list, "推荐帖审核数据");
    }

    /**
     * 获取推荐帖审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:recommend:query')")
    @GetMapping(value = "/{recId}")
    public AjaxResult getInfo(@PathVariable("recId") Long recId)
    {
        return success(recommendService.selectRecommendByRecId(recId));
    }

    /**
     * 新增推荐帖审核
     */
    @PreAuthorize("@ss.hasPermi('system:recommend:add')")
    @Log(title = "推荐帖审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Recommend recommend)
    {
        return toAjax(recommendService.insertRecommend(recommend));
    }

    /**
     * 修改推荐帖审核
     */
    @PreAuthorize("@ss.hasPermi('system:recommend:edit')")
    @Log(title = "推荐帖审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Recommend recommend)
    {
        return toAjax(recommendService.updateRecommend(recommend));
    }

    /**
     * 删除推荐帖审核
     */
    @PreAuthorize("@ss.hasPermi('system:recommend:remove')")
    @Log(title = "推荐帖审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recIds}")
    public AjaxResult remove(@PathVariable Long[] recIds)
    {
        return toAjax(recommendService.deleteRecommendByRecIds(recIds));
    }

    /**
     * 根据用户id查询用户所审核的帖子
     */
    @GetMapping("/findRecByUserId")
    public TableDataInfo findRecByUserId(@RequestParam("userId") Long userId){
        System.out.println(userId);
        List<Recommend> list = recommendService.selectRecommendListByUserId(userId);
        return getDataTable(list);
    }

    @GetMapping("/findRecCountByRecId")
    public AjaxResult findRecCountByRecId(@RequestParam("recId") Long recId){
    	Recommend recommend = recommendService.selectRecommendCountByUserId(recId);
    	return success(recommend);
    }

}
